/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.bomberman.world;

import com.jme3.app.Application;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetManager;
import com.jme3.bullet.BulletAppState;
import com.jme3.input.InputManager;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import de.bomberman.appstate.MainApp;
import de.bomberman.debug.Config;
import java.util.logging.Logger;

/**
 *
 * @author Mats
 */
public class DefaultLevel {

    public static Node levelNode;
    public Node rootNode;
    private final float obstacleSize = 0.5f;
    private AssetManager assetManager;
    private final float floorHeight = 0.1f;
    public static Field[][] field;
    public int levelSize;
    // for physic
    public static BulletAppState bulletAppState;
    private InputManager inputManager;
    private AppStateManager stateManager;
    private MainApp app;
    // Logger
    private static final Logger logger = Logger.getLogger(DefaultLevel.class.getName());

    public DefaultLevel(int levelSize, Application app, Node rootNode) {

        this.levelSize = levelSize;
        this.app = (MainApp) app;
        this.assetManager = app.getAssetManager();
        this.inputManager = app.getInputManager();
        this.stateManager = app.getStateManager();
        this.rootNode = rootNode;

        bulletAppState = new BulletAppState();
        stateManager.attach(bulletAppState);

        // physic debugging
        if (Config.DEBUG_PHYSICS) {
            bulletAppState.getPhysicsSpace().enableDebug(assetManager);
        }


        if (levelSize % 2 == 0) {
            levelSize++;
        }

        // init the field
        field = new Field[levelSize * 2 + 1][levelSize * 2 + 1];

        initFloor();
        initPlayer();

    }

    private void initFloor() {
        Floor floor = new Floor(assetManager, levelSize, floorHeight);
        bulletAppState.getPhysicsSpace().add(floor.getPhysics());
        levelNode = new Node("floor");
        levelNode.attachChild(floor.getFloorGeom());
        levelNode.attachChild(generateObstacles());





    }

    private void initPlayer() {
        Player player = new Player(rootNode, obstacleSize, app);
        bulletAppState.getPhysicsSpace().add(player.getPhysics());
        levelNode.attachChild(player.getPlayerNode());
    }

    private Node generateObstacles() {
        // generate field structure
        int fieldSize = field.length;
        for (int i = 0; i < field.length; i++) {
            for (int j = 0; j < field[i].length; j++) {

                if (i == 0 || i == fieldSize - 1 || j == 0 || j == fieldSize - 1) {
                    field[i][j] = new Field(FIELD_TYPE.BLOCK, obstacleSize, obstacleSize);
                } else if (i % 2 == 0 && j % 2 == 0) {
                    field[i][j] = new Field(FIELD_TYPE.BLOCK, obstacleSize, obstacleSize);
                } else if (!(i == 1 && j == 1) && !(i == 1 && j == 2) && !(i == 2 && j == 1)) {
                    field[i][j] = new Field(FIELD_TYPE.WOOD, obstacleSize, obstacleSize);
                } else {
                    field[i][j] = new Field(FIELD_TYPE.EMPTY, obstacleSize, obstacleSize);
                }
            }

        }

        // create obstacles
        Node obstacles = new Node("obstacles");

        for (int i = 0; i < field.length; i++) {
            for (int j = 0; j < field[i].length; j++) {
                if (field[i][j].getType() == FIELD_TYPE.BLOCK) {
                    Field currentField = field[i][j];
                    currentField.generateField(new Vector3f(i, obstacleSize + floorHeight, j), assetManager, FIELD_TYPE.BLOCK);
                    Geometry obstacle = currentField.getObstacle();
                    bulletAppState.getPhysicsSpace().add(currentField.getPhysics());
                    obstacles.attachChild(obstacle);
                } else if (field[i][j].getType() == FIELD_TYPE.WOOD) {
                    Field currentField = field[i][j];
                    currentField.generateField(new Vector3f(i, obstacleSize + floorHeight, j), assetManager, FIELD_TYPE.WOOD);
                    Geometry obstacle = currentField.getObstacle();
                    bulletAppState.getPhysicsSpace().add(currentField.getPhysics());
                    obstacles.attachChild(obstacle);
                }
            }
        }

        return obstacles;
    }

    public Node getLevelNode() {
        return levelNode;
    }

    public static String getFieldString() {

        StringBuilder sb = new StringBuilder();
        sb.append("\n");
        String sign = "";
        for (int i = 0; i < field.length; i++) {
            for (int j = 0; j < field[i].length; j++) {
                switch (field[j][i].getType()) {
                    case BLOCK:
                        sign = "=";
                        break;
                    case WOOD:
                        sign = "X";
                        break;
                    case BOMB:
                        sign = "O";
                        break;
                    case EMPTY:
                        sign = " ";
                        break;
                }
                sb.append(sign).append("  ");
            }
            sb.append("\n");
        }
        return sb.toString();
    }
}
